home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWPart / Sources / FWCmd.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  9.5 KB  |  370 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWCmd.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWCMD_H
  13. #include "FWCmd.h"
  14. #endif
  15.  
  16. // ----- Framework Includes -----
  17.  
  18. #ifndef FWPART_H
  19. #include "FWPart.h"
  20. #endif
  21.  
  22. #ifndef FWFRAME_H
  23. #include "FWFrame.h"
  24. #endif
  25.  
  26. #ifndef FWSELECT_H
  27. #include "FWSelect.h"
  28. #endif
  29.  
  30. #ifndef FWPRESEN_H
  31. #include "FWPresen.h"
  32. #endif
  33.  
  34. // ----- OS Includes -----
  35.  
  36. #ifndef FWITEXT_H
  37. #include "FWIText.h"
  38. #endif
  39.  
  40. // ----- Foundation Layer -----
  41.  
  42. #ifndef FWMEMMGR_H
  43. #include "FWMemMgr.h"
  44. #endif
  45.  
  46. #ifndef FWSTRS_H
  47. #include "FWStrs.h"
  48. #endif
  49.  
  50. #ifndef FWBNDSTR_H
  51. #include "FWBndStr.h"
  52. #endif
  53.  
  54. // ----- OpenDoc Includes -----
  55.  
  56. // for ODIText
  57. #ifndef SOM_ODPart_xh
  58. #include "Part.xh"
  59. #endif
  60.  
  61. #ifndef SOM_ODFrame_xh
  62. #include "Frame.xh"
  63. #endif
  64.  
  65. #ifndef SOM_ODStorageUnit_xh
  66. #include <StorageU.xh>
  67. #endif
  68.  
  69. #ifndef SOM_ODSession_xh
  70. #include <ODSessn.xh>
  71. #endif
  72.  
  73. // ----- Platform Includes -----
  74.  
  75. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  76. #include <Script.h>
  77. #endif
  78.  
  79. #if defined(FW_BUILD_WIN32S) && !defined(__WINNT_H)
  80. #include <winnt.h>
  81. #endif
  82.  
  83.  
  84. //========================================================================================
  85. //    Runtime Info
  86. //========================================================================================
  87.  
  88. #if FW_LIB_EXPORT_PRAGMAS
  89. #pragma lib_export on
  90. #endif
  91.  
  92. #ifdef FW_BUILD_MAC
  93. #pragma segment FWFrameworkCommands
  94. #endif
  95.  
  96. FW_DEFINE_CLASS_M0(FW_CCommand)
  97.  
  98. //========================================================================================
  99. // FW_CCommand class
  100. //========================================================================================
  101.  
  102. //----------------------------------------------------------------------------------------
  103. // FW_CCommand constructor
  104. //----------------------------------------------------------------------------------------
  105.  
  106. FW_CCommand::FW_CCommand(Environment* ev,
  107.                         ODCommandID id,
  108.                         FW_CFrame* frame,
  109.                          FW_Boolean canUndo) :
  110.     fCommandID(id),
  111.     fCanUndo(canUndo),
  112.     fCausesChange(TRUE),
  113.     fActionType(kODSingleAction),
  114.     fPart(NULL),
  115.     fFrame(frame),
  116.     fSelection(NULL),
  117.     fUndoString(NULL),
  118.     fRedoString(NULL),
  119.     fUndo(NULL),
  120.     fHasAddedAction(FALSE)
  121. {
  122.     FW_ASSERT(fFrame);
  123.     
  124.     fPart = frame->GetPart(ev);
  125.     fSelection = frame->GetPresentation(ev)->GetSelection(ev);
  126.  
  127.     fPart->GetODPart(ev)->Acquire(ev);
  128.  
  129.     fUndo = fPart->GetSession(ev)->GetUndo(ev);
  130.     
  131.     FW_END_CONSTRUCTOR
  132. }
  133.  
  134. //----------------------------------------------------------------------------------------
  135. // FW_CCommand destructor
  136. //----------------------------------------------------------------------------------------
  137.  
  138. FW_CCommand::~FW_CCommand()
  139. {
  140.     FW_START_DESTRUCTOR
  141.  
  142.     Environment *ev = somGetGlobalEnvironment();
  143.     
  144.     fPart->GetODPart(ev)->Release(ev);
  145.  
  146.     PrivDisposeUndoStrings();
  147. }
  148.  
  149. //----------------------------------------------------------------------------------------
  150. // FW_CCommand::PrivDisposeUndoStrings
  151. //----------------------------------------------------------------------------------------
  152.  
  153. void FW_CCommand::PrivDisposeUndoStrings()
  154. {
  155.     if (fUndoString)
  156.         ::DisposeIText(fUndoString);
  157.     if (fRedoString)
  158.         ::DisposeIText(fRedoString);
  159.     
  160.     fUndoString = fRedoString = NULL;
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. // FW_CCommand::PerformCommand
  165. //----------------------------------------------------------------------------------------
  166.  
  167. FW_DECLARE_THROW_POINT(FW_CCommand_PerformCommand_AfterDoIt);
  168. FW_DECLARE_THROW_POINT(FW_CCommand_PerformCommand_AfterChanged);
  169.  
  170. void FW_CCommand::PerformCommand(Environment* ev)
  171. {
  172.     this->DoIt(ev);
  173.     
  174.     if (fCausesChange) {
  175.         FW_TRY {
  176.             FW_CHECK_THROW_POINT(FW_CCommand_PerformCommand_AfterDoIt);
  177.             fPart->Changed(ev);
  178.         }
  179.         FW_CATCH_BEGIN
  180.         FW_CATCH_EVERYTHING () {
  181.             // XXX What if we're linking?
  182.             fCanUndo = FALSE; // XXX is this a good idea? Should we throw again?
  183.         }
  184.         FW_CATCH_END
  185.     }
  186.     
  187.     if (fCanUndo)
  188.     {
  189.         FW_TRY {
  190.             FW_CHECK_THROW_POINT(FW_CCommand_PerformCommand_AfterChanged);
  191.             // Make sure we don't pass NULL menu strings to OpenDoc!
  192.             FW_ASSERT(fUndoString != NULL);
  193.             FW_ASSERT(fRedoString != NULL);
  194.             
  195.             this->AddAction(ev, fActionType, (octet*) &this, sizeof(FW_CCommand*), 
  196.                         fUndoString, fRedoString);
  197.         }
  198.         FW_CATCH_BEGIN
  199.         FW_CATCH_EVERYTHING () {
  200.             // It would be nice if we could put up a dialog saying 
  201.             // this isn't undoable; but we can't continue the 
  202.             // exception or the caller will assume *everything* 
  203.             // failed.
  204.             fCanUndo = false;
  205.         }
  206.         FW_CATCH_END
  207.     }
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. // FW_CCommand::UndoIt
  212. //----------------------------------------------------------------------------------------
  213.  
  214. void FW_CCommand::UndoIt(Environment* ev)
  215. {
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. // FW_CCommand::RedoIt
  220. //----------------------------------------------------------------------------------------
  221.  
  222. void FW_CCommand::RedoIt(Environment* ev)
  223. {
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. // FW_CCommand::CommitDone
  228. //----------------------------------------------------------------------------------------
  229.  
  230. void FW_CCommand::CommitDone(Environment* ev)
  231. {
  232.     // Called by FW_CPart::DisposeActionState before deleting the command
  233.     // The command has been done, and possibly redone
  234.  
  235.     if (fCanUndo)
  236.         this->FreeUndoState(ev);
  237. }
  238.  
  239. //----------------------------------------------------------------------------------------
  240. // FW_CCommand::CommitUndone
  241. //----------------------------------------------------------------------------------------
  242.  
  243. void FW_CCommand::CommitUndone(Environment* ev)
  244. {
  245.     // Called by FW_CPart::DisposeActionState before deleting the command
  246.     // The last action for this command was an Undo
  247.  
  248.     if (fCanUndo)
  249.         this->FreeRedoState(ev);
  250. }
  251.  
  252. //----------------------------------------------------------------------------------------
  253. // FW_CCommand::SaveUndoState
  254. //----------------------------------------------------------------------------------------
  255. void FW_CCommand::SaveUndoState(Environment* ev)
  256. {
  257. }
  258.  
  259. //----------------------------------------------------------------------------------------
  260. // FW_CCommand::SaveRedoState
  261. //----------------------------------------------------------------------------------------
  262. void FW_CCommand::SaveRedoState(Environment* ev)
  263. {
  264. }
  265.  
  266. //----------------------------------------------------------------------------------------
  267. // FW_CCommand::FreeUndoState
  268. //----------------------------------------------------------------------------------------
  269. void FW_CCommand::FreeUndoState(Environment* ev)
  270. {
  271. }
  272.  
  273. //----------------------------------------------------------------------------------------
  274. // FW_CCommand::FreeRedoState
  275. //----------------------------------------------------------------------------------------
  276. void FW_CCommand::FreeRedoState(Environment* ev)
  277. {
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. // FW_CCommand::SetMenuStrings
  282. //----------------------------------------------------------------------------------------
  283.  
  284. void FW_CCommand::SetMenuStrings(Environment* ev,
  285.                                  const FW_CString& undoString,
  286.                                  const FW_CString& redoString)
  287. {
  288.     if (!fCanUndo)
  289.         return;
  290.         
  291.     FW_TRY
  292.     {
  293.         PrivDisposeUndoStrings();    
  294.         fUndoString = ::CreateITextString(undoString);
  295.         fRedoString = ::CreateITextString(redoString);
  296.     }
  297.     FW_CATCH_BEGIN
  298.     FW_CATCH_EVERYTHING()
  299.     {
  300.         PrivDisposeUndoStrings();
  301.         FW_THROW_SAME();
  302.     }
  303.     FW_CATCH_END
  304. }
  305.  
  306. //----------------------------------------------------------------------------------------
  307. // FW_CCommand::SetMenuStrings
  308. //----------------------------------------------------------------------------------------
  309.  
  310. void FW_CCommand::SetMenuStrings(Environment* ev,
  311.                                  char* undoChars,
  312.                                  char* redoChars)
  313. {
  314.     FW_CString255 undoString(undoChars);
  315.     FW_CString255 redoString(redoChars);
  316.     this->SetMenuStrings(ev, undoString, redoString);
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------
  320. // FW_CCommand::AddAction
  321. //----------------------------------------------------------------------------------------
  322.  
  323. void FW_CCommand::AddAction(Environment* ev, ODActionType actionType, 
  324.                             octet* dataPtr, unsigned long dataSize,
  325.                             ODName* undoActionLabel, ODName* redoActionLabel)
  326. {
  327.     // Set up the action data record
  328.     ODActionData actionState;
  329.     actionState._maximum = dataSize;
  330.     actionState._length = dataSize;
  331.     actionState._buffer = dataPtr;
  332.  
  333.     fUndo->AddActionToHistory(ev, fPart->GetODPart(ev),
  334.                               &actionState,
  335.                               actionType,
  336.                               undoActionLabel,
  337.                               redoActionLabel);
  338.     if (dataSize != 0)
  339.         fHasAddedAction = TRUE;    // OpenDoc has a ptr to this command object
  340. }
  341.  
  342. //----------------------------------------------------------------------------------------
  343. // FW_CCommand::IsOKtoEdit
  344. //----------------------------------------------------------------------------------------
  345.  
  346. FW_Boolean FW_CCommand::IsOKtoEdit(Environment* ev)
  347. {
  348.     FW_Boolean result = TRUE;
  349.  
  350.     if (fCausesChange)
  351.     {
  352.         if (fPart->IsReadOnly(ev))
  353.             result = FALSE;
  354.         else
  355.         {
  356.             // Check the frame's link status to see if editing is allowed
  357.             FW_ASSERT(fFrame != NULL);
  358.             ODLinkStatus status = fFrame->GetODFrame(ev)->GetLinkStatus(ev);
  359.             if (status == kODInLinkDestination)
  360.             {
  361.                 // uh-oh, make sure Part::EditInLinkAttempted is called
  362.                 result = fFrame->GetODFrame(ev)->EditInLink(ev);
  363.             }
  364.         }
  365.     }
  366.  
  367.     return result;
  368. }
  369.  
  370.